import { defineStore } from "pinia"
import { ref } from "vue"
import { getUserInfo } from "@/api/admin/user"
import { deleteToken } from "@/composables/cookie/auth"
import { clearTabList } from "@/composables/cookie/tab-list"


export const useUserStore = defineStore('user', () => {
  const userInfo = ref({})

  // 设置用户信息
  function setUserInfo(){
    // 调用获取用户信息接口
    getUserInfo()
      .then(res => {
      if (res.success) {
        userInfo.value = res.data
      }
    })
  }
  //用户登出
  function logout(){
    deleteToken() //清除token
    clearTabList() //清除TabList
    userInfo.value = {}
  }
  
  
  return {
    userInfo,
    setUserInfo,
    logout
  }
}, {
  persist: true
})